<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>测试发送</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h2 style="text-align: center;">测试发送</h2>
<button onclick="send()">发送</button>
<video id="video" width="500px" height="500px" autoplay="autoplay"></video>
<script>
    var video = document.getElementById("video");

    var ws = null;
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        connectWebSocket()
    } else {
        alert('Not support websocket')
    }

    function send() {
        var xx = 'asdsadasdas'
        var b = new Blob([xx])
        ws.send(b)
    }

    function connectWebSocket() {
        ws = new WebSocket("ws://localhost:8081/ws");
    }

    //连接发生错误的回调方法
    ws.onerror = function () {
    };

    //连接成功建立的回调方法
    ws.onopen = function (event) {
        console.log('websocket 连接成功！')
    }

    //接收到消息的回调方法
    ws.onmessage = function (event) {
        try {
            video.stop()
        } catch (e) {
        }
        video.src= window.URL.createObjectURL(event.data)
        //video.play();
    }

    //连接关闭的回调方法
    ws.onclose = function () {
        console.log('websocket连接关闭！')
    }
</script>
</body>
</html>